//
//  MyDisputeAndHelpViewController.m
//  Cunli
//
//  Created by 湛亚 on 15/7/15.
//  Copyright (c) 2015年 lilubin. All rights reserved.
//

#import "MyDisputeAndHelpViewController.h"
#import "MyDataService.h"
#import "NSString+MD5.h"
#import "MJRefresh.h"

@interface MyDisputeAndHelpViewController (){
    
    UITableView *disputeTableView;
    float weight,height;
    NSMutableArray *caseloglistArray;
    int contentPage;
}

@end

@implementation MyDisputeAndHelpViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    contentPage=1;
    height=[UIScreen mainScreen].bounds.size.height;
    weight=[UIScreen mainScreen].bounds.size.width;
    caseloglistArray=[[NSMutableArray alloc]init];
    
    disputeTableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, weight, height-104) style:UITableViewStyleGrouped];
    disputeTableView.dataSource=self;
    disputeTableView.delegate=self;
    disputeTableView.backgroundColor=[UIColor whiteColor];
    disputeTableView.rowHeight=60;
    [self.view addSubview:disputeTableView];
    
    if ([disputeTableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [disputeTableView setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([disputeTableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [disputeTableView setLayoutMargins:UIEdgeInsetsZero];
    }
    
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(0, 0, 28, 28);
    [leftBtn setImage:[UIImage imageNamed:@"regback01"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
    leftBtn.contentEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
    UIBarButtonItem *leftBarItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    self.navigationItem.leftBarButtonItem = leftBarItem;
    
    [self getCallRecordWithPage:1 andIsRefresh:YES];
    [self addRefreshAttribute];
}

//初始化该控制器
- (instancetype)initWithmyType:(myType)type
{
    self = [super init];
    if (self) {
        _requestType = type;
    }
    return self;
}

- (void)addRefreshAttribute
{
    __weak typeof(self) weakSelf = self;
    [disputeTableView addLegendHeaderWithRefreshingBlock:^{
        [weakSelf reloadDataToNew:YES]; //刷新
    }];
    [disputeTableView addLegendFooterWithRefreshingBlock:^{
        [weakSelf reloadDataToNew:NO]; //加载更多
    }];
}

- (void)reloadDataToNew:(BOOL)new{
    
    if (new) {
        contentPage=1;
        [self getCallRecordWithPage:1 andIsRefresh:YES];
    }else{
        contentPage=contentPage+1;
        [self getCallRecordWithPage:contentPage andIsRefresh:NO];
    }
}

-(void)goBack{
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)getCallRecordWithPage:(int)page andIsRefresh:(BOOL)isRefresh{
    
    NSMutableDictionary *CheckParams =[[NSMutableDictionary alloc]init];
    [CheckParams setObject:[Config getOwnUserID] forKey:@"userid"];
    [CheckParams setObject:[NSString stringWithFormat:@"%d",page] forKey:@"page"];
    [CheckParams setObject:@"10" forKey:@"pagesize"];
    [CheckParams setObject:[NSString stringWithFormat:@"%d",_requestType]  forKey:@"type"];
    NSString *resultSign = [NSString getSignFromParam:CheckParams andSession:[Config getOwnSession]];
    [CheckParams setObject:resultSign forKey:@"sign"];
    [MyDataService requestURL:@"own_caselog.php" httpMethod:@"GET" params:CheckParams fileDatas:nil complection:^(id result) {
        
        NSLog(@"%@",result);
        [disputeTableView.legendHeader endRefreshing];
        [disputeTableView.legendFooter endRefreshing];
        
        if (![[result objectForKey:@"response"] isEqualToString:@"own_caselog"]) {
            
            MBProgressHUD *hud1 = [Config createHUD];
            hud1.mode = MBProgressHUDModeCustomView;
            hud1.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"error"]];
            hud1.labelText = @"获取详情失败!";
            [hud1 hide:YES afterDelay:1];
            return ;
        }
        
        NSDictionary *dataDic=[result objectForKey:@"data"];
        NSArray *caselistArray=[dataDic objectForKey:@"caseloglist"];
        
        if (caselistArray.count==0) {
            if (!isRefresh) {
                MBProgressHUD *hud = [Config createHUD];
                hud.mode = MBProgressHUDModeCustomView;
                hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"done"]];
                hud.labelText = @"没有更多数据";
                [hud hide:YES afterDelay:1];
            }
            [disputeTableView.legendFooter setState:MJRefreshFooterStateNoMoreData];
            return ;
        }
        
        if (isRefresh) {
            [caseloglistArray removeAllObjects];
        }
        for (NSDictionary *dic in caselistArray) {
            [caseloglistArray addObject:[dic objectForKey:@"message"]];
        }
        [disputeTableView reloadData];
    }failureAction:^{
        
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return caseloglistArray.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellIdentifier=@"cell";
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if(!cell){
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    NSString *messageStr=[caseloglistArray objectAtIndex:indexPath.row];
    NSArray *listItems = [messageStr componentsSeparatedByString:@","];
    UILabel *messageLab=[[UILabel alloc]initWithFrame:CGRectMake(15, 5,weight -30, 50)];
    messageLab.text=[listItems firstObject];
    messageLab.numberOfLines=0;
    [cell addSubview:messageLab];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 1;
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
